//
//  MInfomationViewController.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/11/7.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MInfomationViewController.h"
#import "MDDataBaseUtil.h"
#import "MUserModel.h"
#import "MVIPCardService.h"
#import "MVIPCardModel.h"
#import "MOperatePwdViewController.h"
#import "MApplyOutViewController.h"
#import "MViewUtil.h"
@interface MInfomationViewController ()<UITableViewDataSource, UITableViewDelegate> {

    MOperatePwdViewController *operatePwdVC;
    UIView *blackBackView;
    UITextField *inputPwdTxt;
    MApplyOutViewController *applyOutVC;
    
    
}
- (IBAction)backAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *accountLabel;
@property (weak, nonatomic) IBOutlet UILabel *IDLabel;
@property (weak, nonatomic) IBOutlet UILabel *coinLabel;
@property (nonatomic, strong) MUserModel *userModel;
@property (nonatomic, strong) NSMutableArray *VIPCardMutArray;
@property (weak, nonatomic) IBOutlet UIButton *outBtn;
@property (weak, nonatomic) IBOutlet UIButton *setPwdBtn;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
- (IBAction)setOperatePwdAction:(UIButton *)sender;
- (IBAction)outcomeBtnAction:(UIButton *)sender;
@property (nonatomic, strong) UIView *inputPasswordView;

@end

@implementation MInfomationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.VIPCardMutArray = [NSMutableArray array];
    
    [self initButton];
    self.myTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    

}
- (void)initButton {
    [self.outBtn.layer setBorderWidth:1];
    [self.outBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.outBtn.layer setCornerRadius:3];
    [self.outBtn.layer setMasksToBounds:YES];
    
    [self.setPwdBtn.layer setBorderWidth:1];
    [self.setPwdBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.setPwdBtn.layer setCornerRadius:3];
    [self.setPwdBtn.layer setMasksToBounds:YES];
    
   
//    [self.inputPasswordView setHidden:YES];

    
}
- (void)viewWillAppear:(BOOL)animated {
    
    self.accountLabel.text = [MDDataBaseUtil userLoginName];
    self.IDLabel.text = [MDDataBaseUtil userID];
    self.coinLabel.text = [NSString stringWithFormat:@"账户金额：%@ 元",[MDDataBaseUtil userMoney]];
    
    [[MVIPCardService sharedClient] getVipCardExchangeDealLogWithUserID:[[MDDataBaseUtil userID] integerValue] complention:^(NSMutableArray *result){

        self.VIPCardMutArray = result;
        [self.myTableView reloadData];
        
    } failure:^(NSString *error){
    
        
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 60)];
    
    headerView.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 6, self.myTableView.bounds.size.width, 30)];
    titleLabel.text = @"账号明细记录";
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont fontWithName:nil size:16.0];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [headerView addSubview:titleLabel];
    
    UILabel *inOrExLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 60, 30)];
    inOrExLabel.text = @"收入/支出";
    inOrExLabel.backgroundColor = WHITESMOLKE;
    inOrExLabel.textAlignment = NSTextAlignmentCenter;
    inOrExLabel.font = [UIFont fontWithName:nil size:12.0];
    inOrExLabel.textColor = [UIColor blackColor];
    [headerView addSubview:inOrExLabel];
    
    UILabel *moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 50, 30)];
    moneyLabel.text = @"金额(元)";
    moneyLabel.backgroundColor = WHITESMOLKE;
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    moneyLabel.font = [UIFont fontWithName:nil size:12.0];
    moneyLabel.textColor = [UIColor blackColor];
    [headerView addSubview:moneyLabel];

    UILabel *accountMoneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(110, 30, 70, 30)];
    accountMoneyLabel.text = @"账户金额(元)";
    accountMoneyLabel.backgroundColor = WHITESMOLKE;
    accountMoneyLabel.textAlignment = NSTextAlignmentCenter;
    accountMoneyLabel.font = [UIFont fontWithName:nil size:12.0];
    accountMoneyLabel.textColor = [UIColor blackColor];
    [headerView addSubview:accountMoneyLabel];
    
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(180, 30, SCREENWIDTH-180, 30)];
    timeLabel.text = @"操作时间";
    timeLabel.backgroundColor = WHITESMOLKE;
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.font = [UIFont fontWithName:nil size:12.0];
    timeLabel.textColor = [UIColor blackColor];
    [headerView addSubview:timeLabel];
    
    return headerView;

    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    self.myTableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, SCREENWIDTH, 30*self.VIPCardMutArray.count+60);
    return self.VIPCardMutArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellStr = @"indentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        
        UILabel *inOrExLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
        inOrExLabel.textColor = [UIColor blackColor];
        inOrExLabel.font = [UIFont fontWithName:nil size:12.0];
        inOrExLabel.textAlignment = NSTextAlignmentCenter;
        inOrExLabel.tag = 101;
        
        UILabel *moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 50, 30)];
        moneyLabel.textColor = [UIColor blackColor];
        moneyLabel.font = [UIFont fontWithName:nil size:12.0];
        moneyLabel.textAlignment = NSTextAlignmentCenter;
        moneyLabel.tag = 102;
        
        UILabel *accountMoneyLable = [[UILabel alloc]initWithFrame:CGRectMake(110, 0, 70, 30)];
        accountMoneyLable.textColor = [UIColor blackColor];
        accountMoneyLable.font = [UIFont fontWithName:nil size:12.0];
        accountMoneyLable.textAlignment = NSTextAlignmentCenter;
        accountMoneyLable.tag = 103;
        
        UILabel *operationTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(180, 0, SCREENWIDTH-180, 30)];
        operationTimeLabel.textColor = [UIColor blackColor];
        operationTimeLabel.font = [UIFont fontWithName:nil size:12.0];
        operationTimeLabel.textAlignment = NSTextAlignmentCenter;
        operationTimeLabel.tag = 104;
        [cell.contentView addSubview:inOrExLabel];
        [cell.contentView addSubview:moneyLabel];
        [cell.contentView addSubview:accountMoneyLable];
        [cell.contentView addSubview:operationTimeLabel];
        
    }
    MVIPCardModel *model = [self.VIPCardMutArray objectAtIndex:indexPath.row];
    ((UILabel *)[cell.contentView viewWithTag:101]).text = model.operate;
    ((UILabel *)[cell.contentView viewWithTag:102]).text = model.inMoney;
    ((UILabel *)[cell.contentView viewWithTag:103]).text = model.balance;
    NSString *string = [model.createTime substringToIndex:10];
    NSString *string2 = [model.createTime substringFromIndex:10];
    ((UILabel *)[cell.contentView viewWithTag:104]).text = [NSString stringWithFormat:@"%@ %@",string,string2];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)backAction:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)setOperatePwdAction:(UIButton *)sender {
    
    operatePwdVC = [[MOperatePwdViewController alloc]init];
    operatePwdVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:operatePwdVC animated:YES completion:nil];
}

- (IBAction)outcomeBtnAction:(UIButton *)sender {
    
    blackBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREENWIDTH, SCREENHEIGHT-20)];
    blackBackView.backgroundColor = [UIColor blackColor];
//    blackBackView.backgroundColor = LIGHTGREEN;
    [blackBackView setAlpha:0.3];
    [self.view addSubview:blackBackView];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickBackHidden)];
    [blackBackView addGestureRecognizer:tapGesture];
    
    [self _initInputPasswordView];
    
//    [UIView animateWithDuration:0.5 animations:^{
//    
//        CGRect originFrame = self.inputPasswordView.frame;
//        originFrame.origin.y = 200;
//        self.inputPasswordView.frame = originFrame;
//    }];
    [self inputPwdViewAnimate];
    
    
}
- (void)_initInputPasswordView {

    self.inputPasswordView = [[UIView alloc]initWithFrame:CGRectMake(30, 150, SCREENWIDTH-60, 150)];
    self.inputPasswordView.backgroundColor = [UIColor whiteColor];
    [self.inputPasswordView.layer setCornerRadius:7.0];
    [self.inputPasswordView.layer setMasksToBounds:YES];
    [self.view addSubview:self.inputPasswordView];
    [self.view bringSubviewToFront:self.inputPasswordView];
    
    UILabel *operateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 100, 44)];
    operateLabel.text = @"操作密码：";
    operateLabel.textAlignment = NSTextAlignmentCenter;
    operateLabel.font = [UIFont fontWithName:nil size:15.0];
    operateLabel.textColor = [UIColor blackColor];
    [self.inputPasswordView addSubview:operateLabel];
    
    inputPwdTxt = [[UITextField alloc]initWithFrame:CGRectMake(100, 30, self.inputPasswordView.frame.size.width-100, 44)];
    [self.inputPasswordView addSubview:inputPwdTxt];
    [inputPwdTxt setSecureTextEntry:YES];
    [inputPwdTxt setFont:[UIFont fontWithName:nil size:15.0]];
    inputPwdTxt.placeholder = @"请输入操作密码";
    [inputPwdTxt setBorderStyle:UITextBorderStyleNone];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(10, 74, self.inputPasswordView.frame.size.width-20, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.inputPasswordView addSubview:lineView];
    
    UIButton *okButton = [[UIButton alloc]initWithFrame:CGRectMake(5, self.inputPasswordView.frame.size.height-40, (self.inputPasswordView.frame.size.width)/2-10, 30)];
    //LIGHTGREEN
    [okButton setTitle:@"确定" forState:UIControlStateNormal];
    [okButton setTitleColor:WHITESMOLKE forState:UIControlStateNormal];
    [okButton.layer setCornerRadius:3.0];
    [okButton.layer setMasksToBounds:YES];
    [okButton setBackgroundColor:BULEHE];
    [self.inputPasswordView addSubview:okButton];
    [okButton addTarget:self action:@selector(doOutcomeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *cancleButton = [[UIButton alloc]initWithFrame:CGRectMake((self.inputPasswordView.frame.size.width)/2+5, self.inputPasswordView.frame.size.height-40, (self.inputPasswordView.frame.size.width)/2-10, 30)];
    
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancleButton setTitleColor:WHITESMOLKE forState:UIControlStateNormal];
    [cancleButton.layer setCornerRadius:3.0];
    [cancleButton.layer setMasksToBounds:YES];
    [cancleButton setBackgroundColor:BULEHE];
    [self.inputPasswordView addSubview:cancleButton];
    [cancleButton addTarget:self action:@selector(doCancleAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)doOutcomeAction:(id)sender {
    [[MVIPCardService sharedClient]validateVipCardWithUserID:[[MDDataBaseUtil userID] integerValue] password:inputPwdTxt.text comletion:^(NSString *result){
    
        if ([result isEqualToString:@"200"]) {
            applyOutVC = [[MApplyOutViewController alloc]init];
            applyOutVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            [self presentViewController:applyOutVC animated:YES completion:nil];
            [self clickBackHidden];
        }
    } failure:^(NSString *error){
    
        [[MViewUtil instance] showShortDurationMessage:error targetView:self.view];
    }];
    
}
- (void)doCancleAction:(id)sender {

    [self clickBackHidden];
}
- (void)inputPwdViewAnimate {

    CATransition *transiton = [CATransition animation];
    transiton.duration = 0.5;
    transiton.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    transiton.type = kCATransitionFromBottom;
    [transiton setType:kCATransitionMoveIn];
    transiton.subtype = kCATransitionFromBottom;
    transiton.delegate = self;
    [self.inputPasswordView.layer addAnimation:transiton forKey:nil];
    
}
- (void)clickBackHidden{
    
    [UIView animateWithDuration:0.5 animations:^{
    
       
        
        CGRect originFrame = self.inputPasswordView.frame;
        originFrame.origin.y = SCREENHEIGHT+10;
        self.inputPasswordView.frame = originFrame;
        
         [blackBackView setHidden:YES];
    }];

    
    
//    [self.inputPasswordView setHidden:YES];
    [inputPwdTxt resignFirstResponder];
}
@end
